package com.sky.controller.admin;


import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Api(tags = "用户订单接口")
@Slf4j
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("管理员{}，查询历史订单", BaseContext.getCurrentId());
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各类订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> orderStatistics(){
        return Result.success(orderService.orderStatistics());
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> orderDetails(@PathVariable Long id){
        return Result.success(orderService.getOrderDetail(id));
    }

    /**
     * 订单确认
     * @param orders
     * @return
     */
    @PutMapping("/confirm")
    public Result<String> confirm(@RequestBody Orders orders){
        orderService.updateOrder(orders, Orders.CONFIRMED);
        return Result.success();
    }

    /**
     * 订单拒接
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("拒绝订单{}，原因{}", ordersRejectionDTO.getId(), ordersRejectionDTO.getRejectionReason());
        orderService.updateOrder(Orders.builder().id(ordersRejectionDTO.getId()).rejectionReason(ordersRejectionDTO.getRejectionReason()).build(), Orders.CANCELLED);
        return Result.success();
    }

    /**
     * 订单取消
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    public Result<String> cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("取消订单{}，原因{}", ordersCancelDTO.getId(), ordersCancelDTO.getCancelReason());
        orderService.updateOrder(Orders.builder().cancelReason(ordersCancelDTO.getCancelReason()).id(ordersCancelDTO.getId()).build(), Orders.CANCELLED);
        return Result.success();
    }

    /**
     * 订单派送
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    public Result<String> delivery(@PathVariable Long id){
        orderService.updateOrder(Orders.builder().id(id).build(), Orders.DELIVERY_IN_PROGRESS);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    public Result<String> complete(@PathVariable Long id){
        orderService.updateOrder(Orders.builder().id(id).build(), Orders.COMPLETED);
        return Result.success();
    }


}
